//
//  UploadDataViewController.m
//  hospitalVIP
//
//  Created by Xiaoyan on 13-4-19.
//  Copyright (c) 2013年 MEETStudio. All rights reserved.
//

#import "UploadDataViewController.h"

static ASIHTTPConnection * uploadData_connect;

@interface UploadDataViewController ()

@end

@implementation UploadDataViewController


@synthesize weightTextField;
@synthesize bloodTextField,shuzhangyaTextField,shousuoyaTextField,niaoTextField;

- (void)dealloc{
    
    if (uploadData_connect != nil) {
        uploadData_connect.connectionDelegate = nil;
        [uploadData_connect release];
        uploadData_connect = nil
        ;
    }
    
    [myScrolView release];
    
    [weightTextField release];
    [bloodTextField release];
    [shuzhangyaTextField release];
    [shousuoyaTextField release];
    [niaoTextField release];
    
    [super dealloc];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
           }
    return self;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    
    if (textField == shousuoyaTextField || textField == shuzhangyaTextField || textField == bloodTextField || textField ==  niaoTextField ) {
        [myScrolView setContentSize:CGSizeMake(myScrolView.frame.size.width,myScrolView.frame.size.height + 200)];
       // [myScrolView setContentOffset:CGPointMake(0, 100)];
        [myScrolView setContentOffset:CGPointMake(0, textField.frame.origin.y -50) animated:YES];
       // [myScrolView scrollRectToVisible:CGRectMake(0, textField.frame.origin.y+40, 320, 100) animated:YES];
    }
    
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    
    [myScrolView setContentSize:CGSizeMake(myScrolView.frame.size.width,myScrolView.frame.size.height)];
    //[myScrolView setContentOffset:CGPointMake(0, 200)];
    [myScrolView scrollRectToVisible:CGRectMake(0, 0, 320, 100) animated:YES];
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    if (textField == weightTextField) {
        [bloodTextField becomeFirstResponder];
    }
    
    if (textField == bloodTextField) {
        [niaoTextField becomeFirstResponder];
    }
    
    if (textField == niaoTextField) {
        [shuzhangyaTextField becomeFirstResponder];
    }
    if (textField == shuzhangyaTextField) {
        [shousuoyaTextField becomeFirstResponder];
    }
    if (textField == shousuoyaTextField) {
        
        [self uploadHealthData];
    }
    return YES;
}


- (IBAction)backGroundTap:(id)sender{
    
    [weightTextField resignFirstResponder];
    [bloodTextField resignFirstResponder];
    [shuzhangyaTextField resignFirstResponder];
    [shousuoyaTextField resignFirstResponder];
    [niaoTextField resignFirstResponder];
    [myScrolView setContentSize:CGSizeMake(myScrolView.frame.size.width,myScrolView.frame.size.height)];
    //[myScrolView setContentOffset:CGPointMake(0, 200)];
    [myScrolView scrollRectToVisible:CGRectMake(0, 0, 320, 100) animated:YES];
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self setTItleWith:@"上传数据"];
   
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"bg_pattern_wood" ofType:@"png"];
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:path]];
    
    UIButton *rightBt = [[UIButton alloc]initWithFrame:CGRectMake(0, 7, 50, 30)];
    [rightBt.titleLabel setFont:[UIFont systemFontOfSize:17]];
    [rightBt.titleLabel setTextColor:[UIColor whiteColor]];
    [rightBt setTitle:@"上传" forState:UIControlStateNormal];
    [rightBt addTarget:self action:@selector(uploadHealthData) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:rightBt];
    [self.navigationItem setRightBarButtonItem:rightButton];
    [rightBt release];
    [rightButton release];


    
}

- (void)uploadHealthData{
    
    if (shuzhangyaTextField.text == nil && shousuoyaTextField.text == nil && bloodTextField.text == nil && weightTextField.text == nil && niaoTextField.text == nil) {
        
        [LoadingView showDurationMessageLoadingViewWith:1 message:@"上传内容不能为空"];
        return;
        
        
    }
    
    
    
    [LoadingView showDefaultLoadingView];
    //NSString *urlString = @"http://211.83.105.83/test2/MobileHealth/SaveHealthDataAction.aspx";
    NSString *urlString = [NSString stringWithFormat:@"%@/MobileHealth/SaveHealthDataAction.aspx",BASEURL];
    
	NSURL *url = [NSURL URLWithString:urlString];
    
    NSString *userName = [[NSUserDefaults standardUserDefaults] valueForKey:USER_NAME];
    int userId = [[[NSUserDefaults standardUserDefaults] valueForKey:USER_ID] intValue];
    
    NSString *DiastolicPressure = @"0";
    if (![shuzhangyaTextField.text isEqualToString:@""]) {
        DiastolicPressure = shuzhangyaTextField.text;
    }
    
    NSString *SystolicPressure = @"0";
    if (![shousuoyaTextField.text isEqualToString:@""]) {
        SystolicPressure = shousuoyaTextField.text;
    }
    
    NSString *BloodSugar = @"0";
    if (![bloodTextField.text isEqualToString:@""]) {
        BloodSugar = bloodTextField.text;
    }
    
    NSString *Weight = @"0";
    if (![weightTextField.text isEqualToString:@""]) {
        Weight = weightTextField.text;
    }
    
    NSString *UriProtein = @"0";
    if (![niaoTextField.text isEqualToString:@""]) {
        UriProtein = niaoTextField.text;
    }
    
    
    NSArray *keys = [NSArray arrayWithObjects:@"UserID",@"UserName",@"DiastolicPressure",@"SystolicPressure",@"BloodSugar",@"Weight",@"UriProtein", nil];
    
    NSArray *objects = [NSArray arrayWithObjects:[NSNumber numberWithInt:1],@"admin888",DiastolicPressure,SystolicPressure,BloodSugar,Weight,UriProtein, nil];
    
    NSDictionary *theRequestDictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
    
    NSString *theBodyString = [[CJSONSerializer serializer] serializeDictionary:theRequestDictionary];
    CLog(@"theBodyString  %@", theBodyString);
    
    //  //
    NSData *_jsonData = [theBodyString dataUsingEncoding:NSUTF8StringEncoding];
    
    if (uploadData_connect != nil) {
        uploadData_connect.connectionDelegate = nil;
        [uploadData_connect release];
        uploadData_connect = nil
        ;
    }
    uploadData_connect = [[ASIHTTPConnection alloc] initPOSTWithURL:url jsonData:_jsonData];
    uploadData_connect.connectionDelegate = self;
    
    [uploadData_connect startAsynchronous];
    
}

- (void)requestFailed:(ASIHTTPConnection *)connection error:(NSError *)error URL:(NSURL *)url info:(NSDictionary *)info{
    
    [LoadingView showDurationMessageLoadingViewWith:1 message:@"上传失败，请稍后重试"];
}

- (void)requestFinished:(ASIHTTPConnection *)connection resultDict:(NSDictionary *)dict URL:(NSURL *)url info:(NSDictionary *)info{
    
    weightTextField.text = nil;
    bloodTextField.text = nil;
    niaoTextField.text = nil;
    shuzhangyaTextField.text = nil;
    shousuoyaTextField.text = nil;
    
    [LoadingView showDurationMessageLoadingViewWith:1 message:@"上传成功"];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
